home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / pc / windows / qtw_201 / setup / samples / eachmoov / eachmoov.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-19  |  12.0 KB  |  433 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // EACHMOOV.C   - QuickTime for Windows
  5. //
  6. //                Version 1.0
  7. //
  8. //                (c) Copyright 1988-1994 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. #include <windows.h>
  14. #include <dlgs.h>
  15. #include <commdlg.h>
  16. #include <qtw.h>
  17. #include <dos.h>
  18. #include <direct.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #define ID_TIMER 1
  24. //
  25. // Function declarations.
  26. //
  27. typedef UINT (CALLBACK * OPENDLGHOOKPROC) (HWND, UINT, WPARAM, LPARAM);
  28.  
  29. long FAR PASCAL __export WndProc (HWND, UINT, WPARAM, LPARAM);
  30. UINT CALLBACK __export OpenDlgHookProc (HWND, UINT, WPARAM, LPARAM);
  31. static VOID NEAR MyListAllMovies (char*, char*);
  32. static VOID NEAR MyPlayNextMovie  (VOID);
  33. static VOID NEAR MyProcessTimer( HWND);
  34. //
  35. // Global variables.
  36. //
  37. static MovieFile mfMovie;
  38. static Movie mMovie;
  39. static MovieController mcController;
  40. static FILE* hList;
  41. static HWND hWnd;
  42. static char szAppName[] = "EACHMOOV";
  43.  
  44. // WinMain
  45. // ---------------------------------------------------------------------
  46. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  47.    LPSTR lpszCmdParam, int nCmdShow)
  48. {
  49.     MSG msg;
  50.     WNDCLASS wndclass;
  51.     char szDrive[_MAX_DRIVE];
  52.     char szDir[_MAX_DIR];
  53.     char szFileTitle[_MAX_PATH];
  54.     char szParm[_MAX_PATH];
  55.     char* pszTemp;
  56.     OPENFILENAME ofn;
  57.  
  58.     // If parameter is missing, use the standard Open Dialog
  59.     // to get the root directory
  60.  
  61.     if (lstrlen (lpszCmdParam) == 0) {
  62.         _getcwd (szDir, sizeof (szDir));
  63.         szParm[0] = '\0';
  64.         memset (&ofn, 0, sizeof(OPENFILENAME));
  65.         ofn.lStructSize = sizeof(OPENFILENAME);
  66.         ofn.hwndOwner = 0;
  67.         ofn.lpstrFilter = "Movies\0all\0\0";
  68.         ofn.nFilterIndex = 1;
  69.         ofn.lpstrFile = szParm;
  70.         ofn.nMaxFile = sizeof(szParm);
  71.         ofn.lpstrFileTitle = szFileTitle;
  72.         ofn.nMaxFileTitle = sizeof(szFileTitle);
  73.         ofn.lpstrInitialDir = szDir;
  74.         ofn.lpstrTitle = "Play Each Movie";
  75.         ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE;
  76.         ofn.hInstance = hInstance;
  77.         ofn.lpTemplateName = "CustomDirectorySelect";
  78.         ofn.lpfnHook = (OPENDLGHOOKPROC) MakeProcInstance ((FARPROC) OpenDlgHookProc, hInstance);
  79.         if (GetOpenFileName (&ofn) == 0)
  80.             return 0;
  81.         FreeProcInstance ((FARPROC) ofn.lpfnHook);
  82.         _strupr (szParm);
  83.     }
  84.  
  85.     // Otherwise, treat the command line parameter as the
  86.     // root directory. We append a dummy file name to assist in the
  87.     // subsequent parsing
  88.  
  89.     else {
  90.         lstrcpy (szParm, lpszCmdParam);
  91.         strcat (szParm, "\\crap");
  92.         _strupr (szParm);
  93.     }
  94.  
  95.     // Establish links to QuickTime for Windows
  96.  
  97.     if (QTInitialize (NULL)) {
  98.         MessageBox (NULL, "QTInitialize failure", szAppName, MB_OK);
  99.         return 0;
  100.     }
  101.  
  102.     // Allocate memory required for playing movies
  103.  
  104.     if (EnterMovies ()) {
  105.         MessageBox (NULL, "EnterMovies failure", szAppName, MB_OK);
  106.         return 0;
  107.     }
  108.  
  109.     // Register and create main window
  110.  
  111.     if (!hPrevInstance) {
  112.         wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  113.         wndclass.lpfnWndProc = WndProc;
  114.         wndclass.cbClsExtra = 0;
  115.         wndclass.cbWndExtra = 0;
  116.         wndclass.hInstance = hInstance;
  117.         wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION);
  118.         wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  119.         wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  120.         wndclass.lpszMenuName = NULL;
  121.         wndclass.lpszClassName = szAppName;
  122.         if (!RegisterClass (&wndclass)) {
  123.             MessageBox (NULL, "RegisterClass failure", szAppName, MB_OK);
  124.             return 0;
  125.         }
  126.     }
  127.  
  128.     hWnd = CreateWindow(szAppName, szAppName, WS_CAPTION | WS_SYSMENU |
  129.         WS_CLIPCHILDREN | WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT,
  130.         CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  131.  
  132.     if (hWnd == NULL) {
  133.         MessageBox (NULL, "CreateWindow failure", szAppName, MB_OK);
  134.         return 0;
  135.     }
  136.  
  137.     // Make a list of all the movies
  138.  
  139.     pszTemp = _tempnam ("c:\\", "each");
  140.     hList = fopen (pszTemp, "w+");
  141.     _splitpath (szParm, szDrive, szDir, NULL, NULL);
  142.     if (strlen (szDrive) > 0)
  143.         _chdrive ((int) (szDrive[0] - '@'));
  144.     if (strlen (szDir) > 1)
  145.         szDir[strlen(szDir) - 1] = 0;
  146.     MyListAllMovies (szDir, ".MOV");
  147.  
  148.     // Open the list of movies and process the first in the list
  149.  
  150.     rewind (hList);
  151.     MyPlayNextMovie ();
  152.  
  153.     // Make the main window visible
  154.  
  155.     ShowWindow (hWnd, nCmdShow);
  156.     UpdateWindow (hWnd);
  157.  
  158.     // Play the movie
  159.  
  160.     while (GetMessage (&msg, NULL, 0, 0)) {
  161.         TranslateMessage (&msg);
  162.         DispatchMessage (&msg);
  163.     }
  164.  
  165.     // Close and delete list of movies
  166.  
  167.     fclose (hList);
  168.     remove (pszTemp);
  169.     free (pszTemp);
  170.  
  171.     // Destroy any old movie controller
  172.  
  173.     if (mcController)
  174.         DisposeMovieController (mcController);
  175.  
  176.     // Destroy any old movie
  177.  
  178.     if (mMovie)
  179.         DisposeMovie (mMovie);
  180.  
  181.     // Cut the connections to QuickTime for Windows
  182.  
  183.     ExitMovies ();
  184.     QTTerminate ();
  185.  
  186.     // Return to Windows
  187.  
  188.     return msg.wParam;
  189. }
  190.  
  191.  
  192. // WndProc
  193. // ---------------------------------------------------------------------
  194. long FAR PASCAL __export WndProc (HWND hWnd, UINT message, WPARAM wParam,
  195.    LPARAM lParam)
  196. {
  197.     PAINTSTRUCT ps;
  198.  
  199.     // Drive the movie controller
  200.  
  201.     MCIsPlayerMessage (mcController, hWnd, message, wParam, lParam);
  202.  
  203.     // Process the windows message
  204.  
  205.     switch (message) {
  206.  
  207.         // In case we have to paint this movie
  208.  
  209.         case WM_PAINT:
  210.             if (!BeginPaint (hWnd, &ps))
  211.                 return 0;
  212.             EndPaint (hWnd, &ps);
  213.             return 0;
  214.  
  215.         // Play next movie
  216.  
  217.         case WM_TIMER:
  218.             MyProcessTimer( hWnd);
  219.  
  220.             return 0;
  221.  
  222.         // All done!
  223.  
  224.         case WM_DESTROY:
  225.             PostQuitMessage (0);
  226.             return 0;
  227.  
  228.     }
  229.  
  230.     // Return to Windows
  231.  
  232.     return DefWindowProc (hWnd, message, wParam, lParam);
  233. }
  234.  
  235.  
  236. // OpenDlgHookProc
  237. // --------------------------------------------------------------------
  238. UINT CALLBACK __export OpenDlgHookProc (HWND hDlg, UINT message,
  239.                                         WPARAM wParam, LPARAM lParam )
  240.  
  241. {
  242.     return FALSE;
  243. }
  244.  
  245.  
  246. // MyListAllMovies
  247. // ---------------------------------------------------------------------
  248. static VOID NEAR MyListAllMovies (char* pszDir, char* pszFilter)
  249.  
  250. {
  251.     char szMovie[512];
  252.     struct find_t c_file;
  253.  
  254.     _chdir (pszDir);
  255.  
  256.     if (_dos_findfirst ("*.*", _A_NORMAL | _A_SUBDIR, &c_file) == 0) {
  257.         do {
  258.             if (c_file.name[0] != '.') {
  259.                 if (c_file.attrib & _A_SUBDIR) {
  260.                     MyListAllMovies (c_file.name, pszFilter);
  261.                 }
  262.                 else {
  263.                     if (strstr (c_file.name, pszFilter) != NULL) {
  264.                         _getcwd (szMovie, sizeof (szMovie));
  265.                         if (strlen (szMovie) > 3)
  266.                             strcat (szMovie, "\\");
  267.                         strcat (szMovie, c_file.name);
  268.                         fputs (szMovie, hList);
  269.                         fputc ('\n', hList);
  270.                     }
  271.                 }
  272.             }
  273.         } while (_dos_findnext (&c_file) == 0);
  274.     }
  275.  
  276.     _chdir ("..");
  277.  
  278. }
  279.  
  280.  
  281. // MyPlayNextMovie
  282. // ---------------------------------------------------------------------
  283. static VOID NEAR MyPlayNextMovie (VOID)
  284.  
  285. {
  286.     RECT rcDesktop, rcMovie;
  287.     char szMovie[512];
  288.     char szTitle[_MAX_PATH];
  289.     OSErr oserr;
  290.     PicHandle ph;
  291.     DIBHandle dh;
  292.  
  293.     // Destroy any old movie controller
  294.  
  295.     if (mcController)
  296.         DisposeMovieController (mcController);
  297.  
  298.     // Destroy any old movie
  299.  
  300.     if (mMovie)
  301.         DisposeMovie (mMovie);
  302.  
  303.     // Instantiate the next movie
  304.  
  305.     if (fgets (szMovie, sizeof (szMovie), hList) == NULL) {
  306.         PostQuitMessage (0);
  307.         return;
  308.     }
  309.     szMovie[strlen(szMovie) - 1] = 0;
  310.     _splitpath (szMovie, NULL, NULL, szTitle, NULL);
  311.     _strlwr (szTitle);
  312.     DebugOutput (DBF_WARNING, "Playing %s", szMovie);
  313.  
  314.     if ((oserr = OpenMovieFile (szMovie, &mfMovie, OF_READ)) != noErr) {
  315.         DebugOutput (DBF_WARNING, "OpenMovieFile failure %ld", oserr);
  316.         goto NextMovie;
  317.     }
  318.  
  319.     oserr = NewMovieFromFile (&mMovie, mfMovie, NULL, NULL, 0, NULL);
  320.     CloseMovieFile (mfMovie);
  321.     if (oserr != noErr) {
  322.         DebugOutput (DBF_WARNING, "NewMovieFromFile failure %ld", oserr);
  323.         goto NextMovie;
  324.     }
  325.  
  326.     // Instantiate the movie controller
  327.  
  328.     GetMovieBox (mMovie, &rcMovie);
  329.     OffsetRect(&rcMovie, -rcMovie.left, -rcMovie.top);
  330.     mcController = NewMovieController (mMovie, &rcMovie,
  331.         mcTopLeftMovie + mcScaleMovieToFit, hWnd);
  332.  
  333.     // Eliminate the grow box
  334.  
  335.     SetRectEmpty (&rcMovie);
  336.     MCDoAction (mcController, mcActionSetGrowBoxBounds, &rcMovie);
  337.  
  338.     // Make the frame just big enough for the movie and center it
  339.     // in the desktop
  340.  
  341.     MCGetControllerBoundsRect (mcController, &rcMovie);
  342.  
  343.     AdjustWindowRect (&rcMovie, WS_CAPTION | WS_OVERLAPPED, FALSE);
  344.     OffsetRect(&rcMovie, -rcMovie.left, -rcMovie.top);
  345.  
  346.     GetWindowRect (GetDesktopWindow (), &rcDesktop);
  347.     SetWindowPos (hWnd, 0,
  348.         max ((rcDesktop.right - rcMovie.right) / 2, 0),
  349.         max ((rcDesktop.bottom - rcMovie.bottom) / 2, 0),
  350.         rcMovie.right, rcMovie.bottom,
  351.         SWP_NOZORDER | SWP_NOACTIVATE);
  352.     SetWindowText (hWnd, szTitle);
  353.  
  354.     // Assert the use of the palette, if any and enable the keyboard intf.
  355.  
  356.     MCDoAction (mcController, mcActionSetFlags, (LPVOID) mcFlagsUseWindowPalette);
  357.     MCDoAction (mcController, mcActionSetKeysEnabled, (LPVOID) TRUE);
  358.  
  359.     // Make the movie active
  360.  
  361.     SetMovieActive (mMovie, TRUE);
  362.     PrerollMovie (mMovie, 0, MAKELFIXED (1, 0));
  363.  
  364.     // We take a few seconds to test the GetMoviePict, PicturetoDIB combination 
  365.     
  366.     ph = NULL, dh = NULL;
  367.     #ifdef _DEBUG
  368.         if ((ph = GetMoviePict (mMovie, 0)) != NULL) {
  369.             if ((dh = PictureToDIB (ph)) != NULL) {
  370.                 oserr = GetMoviesError ();
  371.                 DebugOutput (DBF_WARNING, "PictureToDIB failure %ld", oserr);
  372.             }
  373.         }
  374.         else {
  375.             oserr = GetMoviesError ();
  376.             DebugOutput (DBF_WARNING, "GetMoviePict failure %ld", oserr);
  377.         }
  378.         if (ph != NULL)
  379.             DisposePicture (ph);
  380.         if (dh != NULL)
  381.             GlobalFree (dh);
  382.     #endif
  383.      
  384.     // Start playing
  385.  
  386.     MCDoAction (mcController, mcActionPlay, (LPVOID) MAKELFIXED (1, 0));
  387.  
  388.     // Set a 2.5 second timer which is when we will check up on the
  389.     // movie
  390.  
  391.     NextMovie: SetTimer (hWnd, ID_TIMER, 2500, NULL);
  392. }
  393.  
  394.  
  395. // MyProcessTimer
  396. // ---------------------------------------------------------------------
  397. static VOID NEAR MyProcessTimer( HWND hWnd)
  398. {
  399.     TimeValue tvEnd;
  400.     TimeValue tvNow;
  401.     TimeScale tsMovie;
  402.     DWORD dwTimeLeft;
  403.  
  404.     // Remove any old timer
  405.  
  406.     KillTimer (hWnd, ID_TIMER);
  407.  
  408.     // Check to see if we are at the end of the movie by getting
  409.     // the duration and the current time.
  410.  
  411.     if ( mMovie) {
  412.         tvEnd = GetMovieDuration( mMovie);
  413.         tvNow = MCGetCurrentTime( mcController, NULL);
  414.  
  415.         // Movie is not done yet. Compute the time left in milliseconds
  416.         // and set another timer based on that.
  417.  
  418.         if ( tvEnd - tvNow > 0) {
  419.             tsMovie = GetMovieTimeScale( mMovie);
  420.             dwTimeLeft = ((DWORD) tvEnd - tvNow) * 1000 / tsMovie;
  421.             if ( dwTimeLeft > 2500)
  422.                 SetTimer (hWnd, ID_TIMER, 2500, NULL);
  423.             else SetTimer( hWnd, ID_TIMER, (WORD) dwTimeLeft + 2000, NULL);
  424.         }
  425.  
  426.         // Fire up the next movie if this one is complete
  427.  
  428.         else MyPlayNextMovie ();
  429.  
  430.     }
  431.  
  432. }
  433.